/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.dialogs;

import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import dgbgui.DGBGUI;
import dgbgui.tools.DialogedTool;

@SuppressWarnings("serial")
public class DGBSettingsDialog extends DGBGUIDialog {
    
    public DGBSettingsDialog(DialogedTool tool, DGBGUI.DGBProperties properties) {
        super(tool,"DGB Sttings");
        setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));


        
        JPanel settingsPanel = new JPanel();
        Border panelBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        settingsPanel.setBorder(BorderFactory.createTitledBorder(panelBorder,"Values"));
        settingsPanel.setLayout(new BoxLayout(settingsPanel,BoxLayout.Y_AXIS));
        
        // TODO: Use a more generic approach? Some values should be radios etc.
        for ( Object key : properties.keySet() ) {
            JPanel keyValuePanel = new JPanel();
            
            keyValuePanel.setLayout(new BoxLayout(keyValuePanel, BoxLayout.X_AXIS));
            
            JLabel nameLabel = new JLabel((String)key);
            JTextField value = new JTextField((String)properties.get(key));
            
            keyValuePanel.add(nameLabel);
            keyValuePanel.add(value);

            m_textFieldMap.put(key,value);
            settingsPanel.add(keyValuePanel);
        }
        
        add(settingsPanel);
        
        // Add the OK and Cancel buttons
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel,BoxLayout.X_AXIS));
        m_okButton = new JButton("OK");
        m_cancelButton = new JButton("Cancel");
        buttonsPanel.add(m_okButton);
        buttonsPanel.add(m_cancelButton);

        m_okButton.addActionListener(tool);
        m_cancelButton.addActionListener(tool);

        add(buttonsPanel);
    }
    
    public String getProperty(Object key) {
        return m_textFieldMap.get(key).getText();
        
    }

    private Map<Object,JTextField> m_textFieldMap = new HashMap<Object,JTextField>();
}
